<template>
  <div class="table-type-manager">
    <el-button type="primary" @click="addTableType">添加桌型</el-button>

    <div class="table-type-cards">
      <el-card
          v-for="(tableType, index) in tableTypes"
          :key="index"
          class="table-type-card"
      >
        <div class="table-type-name">{{ tableType.name }}</div>
        <div class="table-type-details">
          <div>最小人数: {{ tableType.minPeople }}</div>
          <div>最大人数: {{ tableType.maxPeople }}</div>
        </div>
        <div class="table-type-actions">
          <el-button type="text" @click="editTableType(index)">编辑</el-button>
          <el-button type="text" class="danger-button" @click="deleteTableType(index)">删除</el-button>
        </div>
      </el-card>
    </div>

    <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="pageNum"
        @current-change="handlePageChange"
    />

    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        width="30%"
    >
      <el-form :model="currentTableType" label-width="100px">
        <el-form-item label="桌型名称">
          <el-input v-model="currentTableType.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="最小人数">
          <el-input v-model.number="currentTableType.minPeople" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="最大人数">
          <el-input v-model.number="currentTableType.maxPeople" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveTableType">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>


<script>
import { onMounted, ref } from 'vue';
import {ElButton, ElCard, ElDialog, ElInput, ElForm, ElFormItem, ElMessage, ElMessageBox} from 'element-plus';
import axios from "axios";

export default {
  name: 'TableTypeManager',
  components: {
    ElButton,
    ElCard,
    ElDialog,
    ElInput,
    ElForm,
    ElFormItem,
  },
  setup() {
    const pageNum = ref(1);
    const pageSize = ref(10);
    const total = ref(0);

    const tableTypes = ref([
      { name: '大桌（5-6人）', minPeople: 5, maxPeople: 6 },
      { name: '包间（9-10人）', minPeople: 9, maxPeople: 10 },
      // 你可以在这里添加更多的桌型
    ]);

    const dialogVisible = ref(false);
    const dialogTitle = ref('');
    const currentTableType = ref({ name: '', minPeople: 0, maxPeople: 0 });

    const getAllTableTypes = () => {
      axios.get(`table/tableType/${pageNum.value}/${pageSize.value}`).then(res => {
        tableTypes.value = res.data.obj.data;
        total.value = res.data.obj.total;
      }).catch(error => {
        console.error('获取桌型数据失败', error);
      });
    };

    const addTableType = () => {
      currentTableType.value = { name: '', minPeople: 0, maxPeople: 0 };
      dialogTitle.value = '添加桌型';
      dialogVisible.value = true;

    };

    const editTableType = (index) => {
      dialogTitle.value = '编辑桌型';
      currentTableType.value = { ...tableTypes.value[index] };
      dialogVisible.value = true;

    };

    const deleteTableType = (index) => {
      // 删除确认对话框
      ElMessageBox.confirm(
          `确认删除桌型【${tableTypes.value[index].name}】吗？`,
          '删除确认',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        axios.delete(`table/deleteTableType/${tableTypes.value[index].id}`).then(res => {
          ElMessage.success(res.data.msg);
          getAllTableTypes();
        }).catch(err=> {
          ElMessage.error('请求失败')
        }
        );
      });
    };

    const handlePageChange = (newPage) => {
      pageNum.value = newPage;
      getAllTableTypes();
    };

    const saveTableType = () => {
      if (dialogTitle.value === '添加桌型') {
        tableTypes.value.push({ ...currentTableType.value });
        axios.post(`table/addTableType`, currentTableType.value).then(res => {
          ElMessage.success(res.data.msg)
          dialogVisible.value = false;
          getAllTableTypes();
        })
      } else {
        const index = tableTypes.value.findIndex(type => type.name === currentTableType.value.name);
        if (index !== -1) {
          tableTypes.value[index] = { ...currentTableType.value };
          axios.post(`table/editTableType`, currentTableType.value).then(res => {
            ElMessage.success(res.data.msg)
            dialogVisible.value = false;
            getAllTableTypes();
          })
        }
      }
      dialogVisible.value = false;
    };

    // 页面加载时获取数据
    onMounted(() => {
      getAllTableTypes();
    });

    return {
      tableTypes,
      pageNum,
      pageSize,
      total,
      dialogVisible,
      dialogTitle,
      currentTableType,
      getAllTableTypes,
      handlePageChange,
      addTableType,
      editTableType,
      deleteTableType,
      saveTableType,
    };
  },
};
</script>


<style scoped>
.table-type-manager {
  padding: 20px;
}

.table-type-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin-top: 20px; /* 卡片的顶部间距 */
}

.table-type-card {
  width: 200px;
  padding: 20px;
}

.table-type-name {
  font-size: 16px;
  margin-bottom: 10px;
}

.table-type-actions {
  display: flex;
  justify-content: space-between;
}

/* 新增：定义危险按钮样式 */
.danger-button {
  color: red; /* 危险按钮的文字颜色 */
}
</style>
